$(function(){
        var username = localStorage.getItem("parameter");
        var password = localStorage.getItem("password");
        var token = localStorage.getItem("token");
        // 创建 URLSearchParams 对象
        var params = new URLSearchParams(window.location.search);
        var base_url_recommend = 'http://127.0.0.1:8080/recommend';
        var base_url_product = 'http://127.0.0.1:8080/product';
        // 获取参数值
        var DayTime = params.get('DayTime');
        // 历史
        earn_response_show_list_h()
        function earn_response_show_list_h(){
                var url = base_url_recommend+"/list/history/";
                $.ajax({
                        url:url,
                        type:"GET",
                        success: function (response){
                                show_list_h(response);
                        }
                })
        }
        function show_list_h(response){

                $.each(response.data,function(index,value){
                        // 分别获取他的productId和avg的值
                        var productId = response.data[index].productId;
                        var count = response.data[index].count;
                        // console.log(count)
                        var div_li = '<div class="col-sm-12"><div class="thumbnail"><img id="imgurl-'+productId+'" src="..." alt="..."><div class="caption"><h3 id="goodsname-'+productId+'"></h3><p id='+count+'>'+count+'</p><p><a href="#" id="btn-'+productId+'" class="btn btn-primary" role="button">进入详情</a></p></div></div></div>'
                        $(".row-h").append(div_li);
                        earn_response_show_img_name(productId);
                })

                
        }
        // 显示其他信息
        function earn_response_show_img_name(productId){
                var url = base_url_product+"/data/";
                        $.ajax({
                                url:url,
                                type:"GET",
                                data:{
                                        productId:productId
                                },
                                success: function (response){
                                        // console.log(response)
                                        var name = response.data.name;
                                        var imgurl = response.data.imageUrl;
                                        // console.log(imgurl);
                                        // console.log(index)
                                        // console.log($("#goodsname").eq(index).text())
                                        $("#goodsname-"+productId).text(name)
                                        $("#imgurl-"+productId).attr("src", imgurl)
                                        
                                        
                                }
                        })
                        
        }
        // 平均
        earn_response_show_list_a()
        function earn_response_show_list_a(){
                var url = base_url_recommend+"/list/average/";
                $.ajax({
                        url:url,
                        type:"GET",
                        success: function (response){
                                // console.log(response);
                                show_list_a(response);
                        }
                })
        }
        function show_list_a(response){

                $.each(response.data,function(index,value){
                        // 分别获取他的productId和avg的值
                        var productId = response.data[index].productId;
                        var avg = response.data[index].avg;
                        var div_li = '<div class="col-sm-12"><div class="thumbnail"><img id="a-imgurl-'+productId+'" src="..." alt="..."><div class="caption"><h3 id="a-goodsname-'+productId+'"></h3><p id='+avg+'>'+avg+'</p><p><a href="#" id="btn-'+productId+'" class="btn btn-primary" role="button">进入详情</a></p></div></div></div>'
                        $(".row-a").append(div_li);
                        earn_response_show_img_name_a(productId);
                })
        }
        function earn_response_show_img_name_a(productId){
                var url = base_url_product+"/data/";
                        $.ajax({
                                url:url,
                                type:"GET",
                                data:{
                                        productId:productId
                                },
                                success: function (response){
                                        var name = response.data.name;
                                        // console.log(name)
                                        var imgurl = response.data.imageUrl;
                                        $("#a-goodsname-"+productId).text(name)
                                        $("#a-imgurl-"+productId).attr("src", imgurl)
                                        
                                        
                                }
                        })
        }

        $(".btn-find").click(function (){
                var daytime = $("#earn_DayTime").val();
                window.location.href = ".//bangdan.html?DayTime="+daytime;

        })
        // 最近
        earn_response_show_list_r()
        function earn_response_show_list_r(){
                var url = base_url_recommend+"/list/recent/";
                $.ajax({
                        url:url,
                        type:"GET",
                        data:{
                                dateTime:DayTime //201405
                        },
                        success: function (response){
                                if (response.data.length === 0){
                                        // alert("没有数据")
                                }else{
                                        console.log(response);
                                        show_list_r(response);
                                }
                                
                        }
                })
        }
        function show_list_r(response){

                $.each(response.data,function(index,value){
                        // 分别获取他的productId和avg的值
                        var productId = response.data[index].productId;
                        var count = response.data[index].count;
                        var div_li = '<div class="col-sm-12"><div class="thumbnail"><img id="r-imgurl-'+productId+'" src="..." alt="..."><div class="caption"><h3 id="r-goodsname-'+productId+'"></h3><p id='+count+'>'+count+'</p><p><a href="#" id="btn-'+productId+'" class="btn btn-primary" role="button">进入详情</a></p></div></div></div>'
                        $(".row-r").append(div_li);
                        earn_response_show_img_name_r(productId);
                })
        }

        function earn_response_show_img_name_r(productId){
                var url = base_url_product+"/data/";
                        $.ajax({
                                url:url,
                                type:"GET",
                                data:{
                                        productId:productId
                                },
                                success: function (response){
                                        var name = response.data.name;
                                        console.log(name)
                                        var imgurl = response.data.imageUrl;
                                        $("#r-goodsname-"+productId).text(name)
                                        $("#r-imgurl-"+productId).attr("src", imgurl)
                                        
                                        
                                }
                        })
        }
        $(document).on("click", ".btn-primary", function() {
                var h3Content = $(this).closest(".thumbnail").find("h3").text();
                var buttonId = $(this).attr("id");
                var ID = buttonId.substring(4);

                
                window.location.href = "./goodsdetails.html?h3Content="+h3Content+"&ID="+ID;
        });


        // 滚动
        var scrollBtnHistory = $('#history_nav');
        var scrollBtnAvg = $('#avg_nav');
        var scrollBtnRecent = $('#recent_nav');
        var row_h = $('#row-h');
        var row_a = $('#row-a');
        var row_r = $('#row-r');

        scrollBtnHistory.on('click', function() {
                $('html, body').animate({
                    scrollTop: row_h.offset().top
                }, 'slow');
        });
        
        scrollBtnAvg.on('click', function() {
                $('html, body').animate({
                    scrollTop: row_a.offset().top
                }, 'slow');
        });
        
        scrollBtnRecent.on('click', function() {
                // var DayTime = $("#earn_DayTime")
                // // alert(DayTime)
                // earn_response_show_list_r()
                $('html, body').animate({
                    scrollTop: row_r.offset().top
                }, 'slow');
        });

})